﻿using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using System.Data.SqlClient;
using Xms.Context;
using Xms.Flow;
using Xms.Infrastructure.Utility;
using Xms.Schema.Domain;
using Xms.Schema.Entity;
using Xms.Sdk.Client;
using Xms.Web.Api.Models;
using Xms.Web.Framework.Context;
using Xms.Web.Framework.Controller;
using Xms.Web.Framework.Mvc;

namespace Xms.Web.Api
{
    /// <summary>
    /// 数据删除接口
    /// </summary>
    [Route("{org}/api/Student/{action}")]
    public class StudentSeaController : ApiControllerBase
    {
        private readonly IEntityFinder _entityFinder;
        private readonly IDataCreater _dataCreater;
        private readonly IDataUpdater _dataUpdater;


        private readonly IDataFinder _dataFinder;
        private readonly IAppContext _appContext;
        public StudentSeaController(IAppContext appContext,IWebAppContext webAppContext
            , IEntityFinder entityFinder
            , IDataCreater dataCreater
            , IDataUpdater dataUpdater
            , IDataFinder dataFinder)
            : base(webAppContext)
        {
            _entityFinder = entityFinder;
            _dataCreater = dataCreater;
            _dataUpdater = dataUpdater;
            _dataFinder = dataFinder;
            _appContext = appContext;
        }

        [Description("设置为公海学员")]
        [HttpPost]
        public IActionResult StudentSea(DeleteEntityRecordModel model)
        {
            if (model.RecordId.IsEmpty())
            {
                return NotSpecifiedRecord();
            }
            Entity entityMetadata = null;
            if (model.EntityName.IsNotEmpty())
            {
                entityMetadata = _entityFinder.FindByName(model.EntityName);
            }
            else
            {
                entityMetadata = _entityFinder.FindById(model.EntityId);
            }
            if (entityMetadata == null)
            {
                return NotFound();
            }
            //设置进入公海
            CRM.Util.DBExecute.ExecuteNonQuery("Update IntentionStudent set IntentionStudentStatus=1 where IntentionStudentId=@IntentionStudentId",new SqlParameter("@IntentionStudentId", model.RecordId[0]));
            return UpdateSuccess();
        }

        [Description("设置为意向学员")]
        [HttpPost]
        public IActionResult IntentionStudent(DeleteEntityRecordModel model)
        {
            if (model.RecordId.IsEmpty())
            {
                return NotSpecifiedRecord();
            }
            Entity entityMetadata = null;
            if (model.EntityName.IsNotEmpty())
            {
                entityMetadata = _entityFinder.FindByName(model.EntityName);
            }
            else
            {
                entityMetadata = _entityFinder.FindById(model.EntityId);
            }
            if (entityMetadata == null)
            {
                return NotFound();
            }
            //设置进入意向
            CRM.Util.DBExecute.ExecuteNonQuery("Update IntentionStudent set IntentionStudentStatus=0 where IntentionStudentId=@IntentionStudentId", new SqlParameter("@IntentionStudentId", model.RecordId[0]));
            return UpdateSuccess();
        }

        [Description("设置为试听学员")]
        [HttpPost]
        public IActionResult TestStudent(DeleteEntityRecordModel model)
        {
            if (model.RecordId.IsEmpty())
            {
                return NotSpecifiedRecord();
            }
            Entity entityMetadata = null;
            if (model.EntityName.IsNotEmpty())
            {
                entityMetadata = _entityFinder.FindByName(model.EntityName);
            }
            else
            {
                entityMetadata = _entityFinder.FindById(model.EntityId);
            }
            if (entityMetadata == null)
            {
                return NotFound();
            }
            //设置进入试听学员
            CRM.Util.DBExecute.ExecuteNonQuery("Update IntentionStudent set IntentionStudentStatus=3 where IntentionStudentId=@IntentionStudentId", new SqlParameter("@IntentionStudentId", model.RecordId[0]));
            return UpdateSuccess();
        }

        [Description("获取上课试听学员列表")]
        [HttpPost]
        public IActionResult GetTestStudent(DeleteEntityRecordModel model)
        {
            //if (model.RecordId.IsEmpty())
            //{
            //    return NotSpecifiedRecord();
            //}
            Entity entityMetadata = null;
            if (model.EntityName.IsNotEmpty())
            {
                entityMetadata = _entityFinder.FindByName(model.EntityName);
            }
            else
            {
                entityMetadata = _entityFinder.FindById(model.EntityId);
            }
            if (entityMetadata == null)
            {
                return NotFound();
            }
            var sql = "";
            if (model.EntityName == "IntentionStudentAttendantRef")
            {
                sql = "select s.Name from IntentionStudentAttendantRef sr left join IntentionStudent s on s.IntentionStudentId = sr.IntentionStudentId where sr.SetClassId ='" + model.EntityId + "'";
            }
            else if(model.EntityName == "IntentionStudentRef")
            {
                sql = "select s.Name from IntentionStudentRef sr left join IntentionStudent s on s.IntentionStudentId = sr.IntentionStudentId where sr.AuditionClassId ='" + model.EntityId + "'";
            }
            using (var dt = CRM.Util.DBExecute.ExecuteDataTable(sql))
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    var name = string.Empty;

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        name += dt.Rows[i]["Name"] + ",";
                    }
                    return SaveSuccess(name.TrimEnd(','));
                }
            }
            return NotFound();
        }


    }
}